let localurl = "http://127.0.0.1:8000/";
let geturl = localurl + "getAllUsers";
let softdelurl = localurl + "getSoftDelUserInfo";
let getstatus = "get", poststatus = "post", clickpage, temp, status_temp, temp_delete;
let length = geturl.length;

$(function () {

    requestdata(geturl, getstatus, true);

    //页码的点击事件
    $(document).on("click", ".page", function () {
        $(".page").removeClass("active");
        $(this).addClass("active");
        let clickpage = $(this).text();
        requestdata(getthisurl(), getstatus, false, clickpage);
        $(".nowpage").text(clickpage);
    });

    //上一页的点击事件
    $(document).on("click", ".last-page", function () {
        let page = $(".active").text();
        pageclick(page, "pre");
    });

    //下一页的点击事件
    $(document).on("click", ".next-page", function () {
        let page = $(".active").text();
        pageclick(page, "next");
    });

    //回收箱点击
    $(document).on("click", ".huishou", function () {
        $(".add-user").css("display", "none");
        $(".huishou").css("display", "none");
        $(".back").css("display", "inline");
        requestdata(softdelurl, getstatus, true);
    });

    //返回键点击
    $(document).on("click", ".back", function () {
        $(".add-user").css("display", "inline");
        $(".huishou").css("display", "inline");
        $(".back").css("display", "none");
        requestdata(geturl, getstatus, true);
    });

    //新增用户的点击
    $(document).on("click", ".add-user", function () {
        $(".add-user-frame").fadeIn(500);
        $("#name_input").remove();
        $("#account_input").remove();
        $("#status_input").val('');
        $("#name").after(`<input id = "name_input" type="text" placeholder="在此输入昵称" />`);
        $("#account").after(`<input id = "account_input" type="text" placeholder="在此输入学号" />`);
        $('input:radio[name="user"]:checked').prop('checked', false);
        $('#post_select').empty();
        status_temp = 1;
    });

    // 单选框的点击事件
    $(document).on('click', "input:radio[name='user']", function () {
        let radio_status = $('input:radio[name="user"]:checked').val();
        if (radio_status != '0') {
            select_status(radio_status);
        } else {
            $('#post_select').empty();
        }
    });

    //状态设置的点击
    $(document).on("click", ".set-state", function () {
        temp = $(this).parent().prev().prev().prev().prev().attr('data-id');
        let name = $(this).parents("ul").find("li").eq(1).html();
        let account = $(this).parents("ul").find("li").eq(2).html();
        let status = $(this).parents("ul").find("li").eq(3).html();

        $(".add-user-frame").fadeIn(500);
        $("#name_input").remove();
        $("#account_input").remove();
        $("#name").after(`<span id ="name_input">${name}</span>`);
        $("#account").after(`<span id ="account_input">${account}</span>`)
        let radio_status;
        $("input:radio[name='user']").each(function () {
            if (($(this).val() === '-1' && status < 0) || ($(this).val() === '0' && status === '0') || ($(this).val() === '1' && status > 0)) {
                $("input:radio[name='user']").each(function () {
                    $(this).prop('checked', false);
                })
                $(this).prop('checked', true);
                radio_status = $(this).val();
                return;
            }
        });
        select_status(radio_status);
        status_temp = 0
    });

    //新增用户
    //设置状态提交的点击
    $(document).on("click", ".submit1", function () {
        // 状态值
        let status_input = $('#post_select').val();
        //获得单选框的val值 
        let checked_status = $('input:radio[name="user"]:checked').val();
        let status_result;


        if (checked_status === '0') {
            status_result = 0;
        } else if (checked_status === '1') {
            status_result = status_input;
        } else if (checked_status === '-1') {
            status_result = -status_input;
        }
        if (status_temp === 1) {
            let name_input = $('#name_input').val();
            let account_input = $('#account_input').val();
            $.ajax({
                type: "post",
                url: localurl + "postAddUser",
                dataType: 'json',
                data: {
                    user_name: name_input,
                    account: account_input,
                    status: status_result
                },
                success: function (data) {
                    if (data.code === 200) {
                        hint("用户新增成功")
                    } else {
                        hint("用户新增失败");
                    }
                }
            });
        } else if (status_temp === 0) {

            $.ajax({
                type: "post",
                url: localurl + "postSetStatus/" + temp,
                dataType: 'json',
                data: {
                    status: status_result
                },
                success: function (res) {

                    if (res.code === 200) {
                        hint("设置状态成功!")
                    } else {
                        hint("设置状态失败!");
                    }
                }
            });
        }
        requestdata(geturl, getstatus, true);
        $('#post_select').empty();
        $(".add-user-frame").fadeOut(500);
    });


    //回收箱恢复按钮的点击
    $(document).on("click", ".recovery", function () {
        temp = $(this).parent().prev().prev().prev().prev().attr('data-id');

        $.ajax({
            type: "get",
            url: localurl + "getRecovery/" + temp,
            dataType: 'json',
            success: function (data) {
                if (data.code === 200) {
                    hint("用户恢复成功!");
                    requestdata(softdelurl, getstatus, true);
                } else {
                    hint("用户恢复失败!");
                }
            }
        });
    });

    //回收箱彻底删除点击
    $(document).on("click", ".thoroughly", function () {
        $(".thoroughly-warning").fadeIn(500);
        temp = $(this).parent().prev().prev().prev().prev().attr('data-id');
        $('#delete').html(`<h4>确定删除 ? 该用户将被<span style="color: red;">彻底</span>删除</h4>`);
        temp_delete = 0;
    });

    //删除，这里也是一个页面！！！！！！！！！！！！！
    $(document).on("click", ".delete", function () {
        $(".thoroughly-warning").fadeIn(500);
        temp = $(this).parent().prev().prev().prev().prev().attr('data-id');
        $('#delete').html(`<h4>确定删除 ? 该用户将被放入<span style="color: green;">回收箱</span>中</h4>`);
        temp_delete = 1;
    });

    //确定删除的点击
    $(document).on("click", ".thoroughly-giveup", function () {
        if (temp_delete === 0) {
            $.ajax({
                type: "get",
                url: localurl + "getHardDelete/" + temp,
                dataType: 'json',
                success: function (res) {
                    if (res.code === 200) {
                        hint("彻底删除用户成功!");
                    } else {
                        hint("彻底删除用户失败!");
                    }
                }
            });

            requestdata(softdelurl, getstatus, true);

        } else if (temp_delete === 1) {
            $.ajax({
                type: "get",
                url: localurl + "getSoftDelete/" + temp,
                dataType: 'json',
                success: function (res) {
                    if (res.code === 200) {
                        hint("用户已被删除!");
                    } else {
                        hint("用户删除失败!");
                    }
                }
            });
            requestdata(geturl, getstatus, true);
        }


        $(".thoroughly-warning").fadeOut(500)
    });

    //重置密码的点击
    $(document).on("click", ".examine", function () {
        temp = $(this).parent().prev().prev().prev().prev().attr('data-id');
        $.ajax({
            type: "get",
            url: localurl + "getResetPass/" + temp,
            dataType: 'json',
            success: function (res) {
                if (res.code === 200) {
                    $(".password-set").fadeIn(500);
                } else {
                    hint("用户密码重置失败!");
                }
            }
        });
    });

    //新增用户取消的点击
    $(document).on("click", ".close", function () {
        $('post_select').empty();
        $(".add-user-frame").fadeOut(500);
    });

    //确定密码重置
    $(document).on("click", ".sure-password", function () {
        $(".password-set").fadeOut(500)
    });

    //取消的点击
    $(document).on("click", ".give-up-thoroughly", function () {
        $(".thoroughly-warning").fadeOut(500)
    })
});


function requestdata(url, status, pagination, page) {
    if (page !== undefined) {
        url += "?page=" + page;
    }
    $.ajax({
        type: status,
        url: url,
        dataType: 'json',
        success: function (data) {
            if (data.code === 200) {
                pulldata(url, data, pagination);
            } else {
                hint("请求失败");
            }
        }
    });
}

function pageclick(page, status) {
    let acount_page = $(".page-box-one .pagez").text();
    let thispage = $(".active");
    let temppage;
    if (status === "next") {
        if (page === (acount_page).toString()) {
            hint("已是最后一页");
            return;
        }
        page++;
        temppage = thispage.next();
    } else if (status === "pre") {
        if (page === '1') {
            hint("已是第一页!");
            return;
        }
        page--;
        temppage = thispage.prev()
    }
    thispage.removeClass("active");
    temppage.addClass("active");
    requestdata(getthisurl(), getstatus, false, page);
    $(".page-box-one .nowpage").text(page);
}

function pulldata(url, data, pagination) {
    $(".ma-bottom").empty();
    let this_pages = data.data.current_page;
    let $li = `<ul>
                    <li>编号</li>
                    <li>用户</li>
                    <li>学号</li>
                    <li>状态</li>
                    <li>操作</li>
                </ul>`;
    $(data.data.data).each(function (index, item) {
        $li += `<ul> 
                    <li data-id = "${item.id}">${(index) + (this_pages - 1) * 7 + 1}</li> 
                    <li>${item.user_name}</li> 
                    <li>${item.account}</li>
                    <li>${item.status}</li>`;
        if (url.substring(0, length) === geturl.substring(0, length)) {
            $li += `<li>
                        <span class="examine">重置密码</span>
                        <span class="set-state">设置状态</span>
                        <span class="delete">删除</span>
                    </li>
                    </ul>`;
        } else if (url.substring(0, length) === softdelurl.substring(0, length)) {
            $li += `<li>
                        <span class="recovery">恢复</span>
                        <span class="thoroughly">彻底删除</span>
                    </li>
                    </ul>`;
        }
    });
    $(".ma-bottom").append($li);
    if (pagination === true) {
        $(".page").remove();
        let pages = data.data.last_page;
        for (let i = 1; i <= pages; i++) {
            let page = `<div class="page pp">${i}</div>`;
            $(".page-box-one>.pp").last().after(page);
        }
        $(".page").eq(0).addClass("active");
        $(".page").css("display", "block");
        $(".page-box-one .pagez").text(pages);
    }
}


function getthisurl() {
    if ($('.huishou').css('display') === 'inline')
        return geturl;
    else return softdelurl;
}

function select_status(status) {
    $.ajax({
        type: getstatus,
        url: localurl + 'getListInfo/' + status,
        dataType: 'json',
        success: function (data) {
            if (data.code === 200) {
                $('#post_select').empty();
                let $option;
                $(data.data).each(function (index, item) {
                    $option += `<option value="${item.id}">${item.user_name}</option>`;
                });
                $('#post_select').append($option);
            } else {
                alert("请求失败");
            }
        }
    });
}

